﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using HZ.MBSM.DBUtility;
public partial class Company_Goodsbid_add : System.Web.UI.Page
{
    public string _Projecitd = "0";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _Projecitd = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["ID"]));
                if (_Projecitd != "1" && _Projecitd != "2")
                {
                    Response.Write("<script language=JavaScript>alert('该项目不存在！');window.close();</script>");
                }
                else
                {

                    showGridView();
                }

            }

        }
    }

    public void showGridView()
    {

        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        if (_Projecitd == "1") //基本
        {
            dbPage.Table = "v_product_goods";                                               //要查询的表明
            dbPage.Key = "goodsid";                                               //表的主键    
            dbPage.Where = strConditions;                                           //查询条件    
            dbPage.SelectField = "GoodsId as GoodsByCompanyId,productname_gjsp as pn,Outlookc_gjsp as ou,Medicinemodel_gjsp as me,Unit_gjsp as un,factor as fa,goodsother_bc as bc,NoPass,case CheckStatus when '1' then '审核通过' when '2' then '审核未通过' when '3' then '纸质材料一致' else '未审核' end as CheckStatus ";//要显示的字段
            dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
            dbPage.PageSize = 10;                                                    //没有显示记录条数   
            dbPage.OrderBy = "productname,medicinemodel,outlookc,goodsid";                  //排序字段
            System.Data.IDataReader dr = dbPage.ExecuteReader();
            GridView1.Columns[6].Visible = false;
            GridView1.DataSource = dr;
            GridView1.DataBind();
            dr.Close();
            dr = null;
        }
        else   //非基本
        {
            dbPage.Table = "GoodsByCompany";                                               //要查询的表明
            dbPage.Key = "GoodsByCompanyId";                                               //表的主键    
            dbPage.Where = strConditions;                                           //查询条件    
            dbPage.SelectField = "GoodsByCompanyId,productname as pn,Outlookc as ou,Medicinemodel as me,Unit_gjsp as un,factor as fa,'' as bc,NoPass,case CheckStatus when '1' then '审核通过' when '2' then '审核未通过' when '3' then '纸质材料一致' else '未审核' end as CheckStatus, CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels ";//要显示的字段
            dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
            dbPage.PageSize = 10;                                                    //没有显示记录条数   
            dbPage.OrderBy = "productname,medicinemodel,outlookc,GoodsByCompanyId";                  //排序字段
            System.Data.IDataReader dr = dbPage.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();
            dr.Close();
            dr = null;
        }

        //dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        //08.18

        db = null;

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }

    public string fctConditions()
    {
        Int64 Account_tjr;
        string strgoodsid = "";
        if (_Projecitd == "2") strgoodsid = "GoodsByCompanyId";
        else strgoodsid = "goodsid";
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];

        string _strWhere = "and  checkstatus=1   and " + strgoodsid + " not in(select goodsid from Project_Bid)";
        if (_Account != null)
        {
            Account_tjr = _Account.AccountId;

            _strWhere += @"and  Accountid_tjr=" + Account_tjr + " ";

            //药品通用名或拼音码

            string _strProductName = SysFun.ToTrim(txtProductName.Text);
            if (_strProductName != "")
            {
                _strWhere += "and  (ProductName like '%" + _strProductName + "%' ) ";
            }

            //txtMedicinemodel
            //剂型
            string _strMedicinemodel = SysFun.ToTrim(txtMedicinemodel.Text);
            if (_strMedicinemodel != "")
            {
                _strWhere += "and  Medicinemodel like '%" + _strMedicinemodel + "%' ";
            }

            //规格
            string _strOutlookc = SysFun.ToTrim(txtOutlookc.Text);
            if (_strOutlookc != "")
            {
                _strWhere += " and Outlookc like '%" + _strOutlookc + "%'";
            }


            //流水号

            string _strGoodsId = SysFun.ToTrim(txtGoodsId.Text);
            if (_strGoodsId != "")
            {
                _strWhere += " And " + strgoodsid + " =" + _strGoodsId + " ";
            }

        }
        if (_strWhere != "")
        {
            _strWhere = "  " + _strWhere.Remove(0, 3);
        }

        return _strWhere;
    }
    //确定添加投标
    protected void Button6_Click(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        int rowcount = 0;
        if (Request.Form["chk"] == null) return;
        string GoodsId = Request.Form["chk"].ToString();
        Db db = new Db();
        System.Data.IDataReader dr;
        string strSql = "select count(*) as cun from project_bid where GoodsId in (" + GoodsId + ") and projectid=" + _Projecitd;
        dr = db.DataReader(strSql);
        while (dr.Read())
        {
            rowcount = Convert.ToInt32(dr["cun"].ToString());
        }

        dr.Close();
        if (rowcount > 0)
        {
            Response.Write("<script language=JavaScript>alert('该记录在投标表中已经存在不能添加');</script>");
        }
        else
        {
            if (_Projecitd == "1")
            {
                strSql = "insert Project_Bid(AccountId,ProjectId,GoodsId) select " + _Account.AccountId + "," + _Projecitd + ",GoodsId  from goods where  GoodsId in(" + GoodsId + ") and GoodsId not in (select goodsid from project_bid where projectid=" + _Projecitd + ")";
            }
            else
            {
                strSql = "insert Project_Bid(AccountId,ProjectId,GoodsId) select " + _Account.AccountId + "," + _Projecitd + ",GoodsByCompanyId  from GoodsByCompany where  GoodsByCompanyId in(" + GoodsId + ") and GoodsByCompanyId not in (select goodsid from project_bid where projectid=" + _Projecitd + ")";

            }
            db.Command(strSql);
            Response.Write("<script language=JavaScript>alert('品种确认成功！');</script>");
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "t", "goodsid in (" + GoodsId + ")", strSql, "../manage/goodslist.aspx");

        }
        dr.Dispose();
        db.Close();
        dr = null;
        db = null;
        showGridView();
    }
    #region IPageDescribe 成员

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
            return _AccountType;
        }
    }

    #endregion
}
